﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;

namespace ElingOA6_DSJCJ.DAL
{
    /// <summary>
    /// 相册类别 数据类
    /// </summary>
    public class AlbumCls
    {
        public AlbumCls()
        {
        }
        /// <summary>
        /// 是否存在该记录
        /// <creator>wangl</creator>
        /// <date>2014-10-13</date>
        /// </summary>
        public bool Exists(string albumClsNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_AlbumCls");
            strSql.Append(" where FAlbumClsNo=@FAlbumClsNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FAlbumClsNo", SqlDbType.NVarChar,30)			
                                        };
            parameters[0].Value = albumClsNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 增加相册类别
        /// <creator>wangl</creator>
        /// <date>2014-10-13</date>
        /// </summary>
        public void AddAlbumCls(Model.AlbumCls albumClsModel, List<string> orgUserList, string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("insert into t_AlbumCls(");
            strSql.Append("FAlbumClsNo,FAlbumClsName)");
            strSql.Append(" values (");
            strSql.Append("@FAlbumClsNo,@FAlbumClsName)");
            SqlParameter[] parameters = {
					new SqlParameter("@FAlbumClsNo", SqlDbType.NVarChar,30),
					new SqlParameter("@FAlbumClsName", SqlDbType.NVarChar,100)};
            parameters[0].Value = albumClsModel.FAlbumClsNo;
            parameters[1].Value = albumClsModel.FAlbumClsName;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);
          
            //增加组织用户
            foreach (string orgUser in orgUserList)
            {
                strSql = new StringBuilder();
                cmds = new CommandInfo();
                strSql.Append("insert into t_AlbumAuth(");
                strSql.Append("FEntityNo,FEmplNo,FAlbumClsNo)");
                strSql.Append(" values (");
                strSql.Append("@FEntityNo,@FEmplNo,@FAlbumClsNo)");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FAlbumClsNo", SqlDbType.NVarChar,30)};
                parameters2[0].Value = entityNo;
                parameters2[1].Value = orgUser;
                parameters2[2].Value = albumClsModel.FAlbumClsNo;

                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }
        }
        /// <summary>
        /// 修改相册类别
        /// <creator>wangl</creator>
        /// <date>2014-07-12</date>
        /// </summary>
        public void EditAlbumCls(Model.AlbumCls albumClsModel,List<string> orgUserList, string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("UPDATE t_AlbumCls SET FAlbumClsName=@FAlbumClsName");
            strSql.Append(" WHERE FAlbumClsNo=@FAlbumClsNo");
            SqlParameter[] parameters = {
				    new SqlParameter("@FAlbumClsName", SqlDbType.NVarChar,100),
                    new SqlParameter("@FAlbumClsNo", SqlDbType.NVarChar,30)
                                        };
            parameters[0].Value = albumClsModel.FAlbumClsName;
            parameters[1].Value = albumClsModel.FAlbumClsNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            //更新相册类别权限
            strSql = new StringBuilder();
            cmds = new CommandInfo();
            strSql.Append("DELETE FROM t_AlbumAuth WHERE FAlbumClsNo=@FAlbumClsNo");
            SqlParameter[] parameters1 = {
                    new SqlParameter("@FAlbumClsNo", SqlDbType.NVarChar,30)
                                        };
            parameters1[0].Value = albumClsModel.FAlbumClsNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters1;
            cmdList.Add(cmds);

            foreach (string orgUser in orgUserList)
            {
                strSql = new StringBuilder();
                cmds = new CommandInfo();
                strSql.Append("insert into t_AlbumAuth(");
                strSql.Append("FEntityNo,FEmplNo,FAlbumClsNo)");
                strSql.Append(" values (");
                strSql.Append("@FEntityNo,@FEmplNo,@FAlbumClsNo)");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FAlbumClsNo", SqlDbType.NVarChar,30)};
                parameters2[0].Value = entityNo;
                parameters2[1].Value = orgUser;
                parameters2[2].Value = albumClsModel.FAlbumClsNo;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }
        }
        /// <summary>
        /// 删除相册类别
        /// <creator>wangl</creator>
        /// <date>2014-07-12</date>
        /// </summary>
        public void DeleteAlbumCls(string albumClsNo,string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            //删除用户组权限
            strSql = new StringBuilder();
            cmds = new CommandInfo();
            strSql.Append("DELETE FROM t_AlbumAuth WHERE FAlbumClsNo=@FAlbumClsNo and FEntityNo=@FEntityNo");
            SqlParameter[] parameters = {
                    new SqlParameter("@FAlbumClsNo", SqlDbType.NVarChar,30),
                    new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)
                                        };
            parameters[0].Value = albumClsNo;
            parameters[1].Value = entityNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);


            //删除相册类别
            strSql = new StringBuilder();
            cmds = new CommandInfo();
            strSql.Append("DELETE FROM t_AlbumCls WHERE FAlbumClsNo=@FAlbumClsNo");
            SqlParameter[] parameters1 = {
                    new SqlParameter("@FAlbumClsNo", SqlDbType.NVarChar,30)
                                        };
            parameters1[0].Value = albumClsNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters1;
            cmdList.Add(cmds);

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// <creator>wangl</creator>
        /// <date>2014-10-11</date>
        /// </summary>
        public ElingOA6_DSJCJ.Model.AlbumCls GetModel(string albumClsNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FAlbumClsNo,FAlbumClsName from t_AlbumCls ");
            strSql.Append(" where FAlbumClsNo=@FAlbumClsNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FAlbumClsNo", SqlDbType.NVarChar,30)			};
            parameters[0].Value = albumClsNo;

            ElingOA6_DSJCJ.Model.AlbumCls model = new ElingOA6_DSJCJ.Model.AlbumCls();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// <creator>wangl</creator>
        /// <date>2014-10-11</date>
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FAlbumClsNo,FAlbumClsName ");
            strSql.Append(" FROM t_AlbumCls ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 得到一个对象实体
        /// <creator>wangl</creator>
        /// <date>2014-10-11</date>
        /// </summary>
        public ElingOA6_DSJCJ.Model.AlbumCls DataRowToModel(DataRow row)
        {
            ElingOA6_DSJCJ.Model.AlbumCls model = new ElingOA6_DSJCJ.Model.AlbumCls();
            if (row != null)
            {
                if (row["FAlbumClsNo"] != null)
                {
                    model.FAlbumClsNo = row["FAlbumClsNo"].ToString();
                }
                if (row["FAlbumClsName"] != null)
                {
                    model.FAlbumClsName = row["FAlbumClsName"].ToString();
                }
            }
            return model;
        }
    }
}
